
#include "DataParser.h"
#include "DataManager.h"
#include "ErrorHandler.h"
#include <QFile>
#include <QDebug>
#include <QtSql/QSqlDatabase>
#include <QStringList>
#include <QJsonDocument>
#include <QJsonObject>

#include <QTime>
#include <QDate>

DataParser::DataParser()
{
}

DataParser::DataParser(QVariantMap data)
{
    m_data = data;
}

void DataParser::setData(QVariantMap data)
{
    m_data = data;
}

QVariantMap DataParser::data()
{
    return m_data;
}

QVector<QString> DataParser::preg_match(QString reg, QString input)
{
    QVector<QString> cmatch1;
    reg = "(" + reg + ")";

    QRegExp rx(reg);
    int pos = rx.indexIn(input);
    cmatch1 = rx.capturedTexts().toVector();
    cmatch1.pop_front();
    if(cmatch1.size() > 0 && cmatch1[0]=="")
        cmatch1.pop_front();
    return cmatch1;
}

QVector<QString> DataParser::relative_reg(QString reg,QString input)
{
    QVector<QString> cmatch1;

    return cmatch1;
}

QString DataParser::implode(QString sep, QVector<QString> val)
{
    QString result = val[0];
    for(int i = 1; i < val.size(); i++){
        result = result + sep + val[i];
    }
    return result;
}

QString DataParser::relative_str(QVariantMap result, QString col, QString rel_col)
{
    if(col.length() <= 0) {
        rel_col = rel_col.trimmed();
        QVector<QString> matches;
        matches = preg_match("[a-z]+|[+-]|[0-9]+",rel_col);
        if(matches.size() > 0){
            QString column_start_from = matches[1];
            int step = matches[3].toInt();
            QString sign1 = matches[2];
            QString sign = sign1;
            if(column_start_from.length() <= 0)
                col = QString().setNum(step);
            else {
                if(!column_start_from.compare("name")) {
                    if(result["columns"].toMap()["name"].toString().length() > 0) {
                        if(!sign.compare("+"))
                            col = QString().setNum(result["columns"].toMap()["name"].toInt() + step);
                        else if(!sign.compare("-"))
                            col = QString().setNum(result["columns"].toMap()["name"].toInt() - step);
                    }
                }
                else if(!column_start_from.compare("code")) {
                    if(result["columns"].toMap()["code"].toString().length() > 0) {
                        if(!sign.compare("+"))
                            col = QString().setNum(result["columns"].toMap()["code"].toInt() + step);
                        else if(!sign.compare("-"))
                            col = QString().setNum(result["columns"].toMap()["code"].toInt() - step);
                    }
                }
                else if(!column_start_from.compare("article")) {
                    if(result["columns"].toMap()["article"].toString().length() > 0) {
                        if(!sign.compare("+"))
                            col = QString().setNum(result["columns"].toMap()["article"].toInt() + step);
                        else if(!sign.compare("-"))
                            col = QString().setNum(result["columns"].toMap()["article"].toInt() - step);
                    }
                }
                else if(!column_start_from.compare("price")){
                    if(result["columns"].toMap()["price"].toString().length() > 0) {
                        if(!sign.compare("+"))
                            col = QString().setNum(result["columns"].toMap()["price"].toInt() + step);
                        else if(!sign.compare("-"))
                            col = QString().setNum(result["columns"].toMap()["price"].toInt() - step);
                    }
                }
                else if(!column_start_from.compare("rrp")) {
                    if(result["columns"].toMap()["rrp"].toString().length() > 0) {
                        if(!sign.compare("+"))
                            col = QString().setNum(result["columns"].toMap()["rrp"].toInt() + step);
                        else if(!sign.compare("-"))
                            col = QString().setNum(result["columns"].toMap()["rrp"].toInt() - step);
                    }
                }
            }
        }
    }
    return col;
}

bool DataParser::reg1(QString value, QString pattern)
{
    return true;
}

QVariantMap DataParser::checkPricelistTemplate(QString filename)
{
    QVariantMap _tmp1, _tmp2;
    QStringList __tmp1;
    QVariantMap result;
    int row, column;
    bool ok = m_dManager.initialize("QMYSQL", "localhost", "markrt", "root", "1", 3306);

    if(!ok) {
        qDebug() << "Not ok";
        return result;
    }

    qDebug() << "======================";
    m_dManager.setQuery("SELECT pricelists_templates FROM user WHERE id='3344'");

    while(m_dManager.next()) {
        QString stringJson = m_dManager.getNext();
        QJsonDocument jsonDoc = QJsonDocument::fromJson(stringJson.toUtf8());
        QVariantMap model = jsonDoc.object().toVariantMap();
        //        qDebug() << "MODEL " << model.size();
        //        qDebug() << "1 " << model["1"];
        //        qDebug() << "2 " << model["2"];
        //        qDebug() << "3 " << model["1"].toMap()["settings"].toMap()["file_name"].toString();
        for(int i = 1; i <= model.size(); i++) {
            model[QString().setNum(i)].toMap()["settings"].toMap()["file_name"] =
                    model[QString().setNum(i)].toMap()["settings"].toMap()["file_name"].toString().replace("\"", "");
            if(preg_match(model[QString().setNum(i)].toMap()["settings"].toMap()["file_name"].toString(), filename).size() > 0) {
                result["settings"] = model[QString().setNum(i)].toMap()["settings"];
                result["service_marks"] = model[QString().setNum(i)].toMap()["service_marks"];
                result["custom_values"] = model[QString().setNum(i)].toMap()["custom_values"];
                QFile in(filename);
                in.open(QIODevice::ReadOnly);
                QString line;
                row = 0;
                result["start_line"] = -1;
                int col=0;

                if(model[QString().setNum(i)].toMap()["columns"].toMap()["name"].toString().length()>0)
                    col++;
                if(model[QString().setNum(i)].toMap()["columns"].toMap()["article"].toString().length()>0)
                    col++;
                if(model[QString().setNum(i)].toMap()["columns"].toMap()["code"].toString().length()>0)
                    col++;
                if(model[QString().setNum(i)].toMap()["columns"].toMap()["price"].toString().length()>0)
                    col++;
                if(model[QString().setNum(i)].toMap()["columns"].toMap()["rrp"].toString().length()>0)
                    col++;

                while(!in.atEnd()) {
                    line = in.readLine();
                    row++;
                    line = line.replace("\"", "");
                    line = line.replace(",,", ", ,");
                    QStringList lineArr = line.split(",");
                    int column = -1;
                    if(row < 18) {
                        qDebug() << ":L " << line << "  arr: " << lineArr;
                        qDebug() << line[line.length()-1];
                        for(int i = 0; i < 256; i++) {
                            if(line[line.length()-2] == QChar(i))
                                qDebug() << "line.length()-2   " << i;
                            if(line[line.length()-1] == QChar(i))
                                qDebug() << "line.length()-1   " << i;
                            if(line[line.length()] == QChar(i))
                                qDebug() << "line.length()   " << i;
                        }
                    }
                    for(int arrIndex = 0; arrIndex < lineArr.size(); arrIndex++) {
                        lineArr[arrIndex] = lineArr[arrIndex].replace(QRegExp("[^A-Za-zА-Яа-я0-9]"), ""); // alfanumerical
                        column++;

                        if(model[QString().setNum(i)].toMap()["columns"].toMap()["name"].toString().length() > 0 &&
                                preg_match(model[QString().setNum(i)].toMap()["columns"].toMap()["name"].toString(), lineArr[arrIndex]).size() > 0 &&
                                !result[QString().setNum(i)].toMap()["columns"].toMap()["name"].toString().compare("")) {
                            if(result["start_line"].toInt() != -1){
                                result["start_line"] = row + 1;
                            }
                            _tmp1 = result["columns"].toMap();
                            _tmp1["name"] = column;
                            result["columns"] = _tmp1;
                            col--;
                        }
                        if(model[QString().setNum(i)].toMap()["columns"].toMap()["article"] != QVariant::Invalid &&
                                preg_match(model[QString().setNum(i)].toMap()["columns"].toMap()["article"].toString(), lineArr[arrIndex]).size() > 0 &&
                                !result[QString().setNum(i)].toMap()["columns"].toMap()["article"].toString().compare("")){
                            if(result["start_line"].toInt() != -1){
                                result["start_line"] = row + 1;
                            }
                            _tmp1 = result["columns"].toMap();
                            _tmp1["article"] = column;
                            result["columns"] = _tmp1;
                            col--;
                        }
                        if(model[QString().setNum(i)].toMap()["columns"].toMap()["code"] != QVariant::Invalid &&
                                preg_match(model[QString().setNum(i)].toMap()["columns"].toMap()["code"].toString(), lineArr[arrIndex]).size() > 0 &&
                                !result[QString().setNum(i)].toMap()["columns"].toMap()["code"].toString().compare("")){
                            if(result["start_line"].toInt() != -1){
                                result["start_line"] = row + 1;
                            }
                            _tmp1 = result["columns"].toMap();
                            _tmp1["code"] = column;
                            result["columns"] = _tmp1;
                            col--;
                        }
                        if(model[QString().setNum(i)].toMap()["columns"].toMap()["price"] != QVariant::Invalid &&
                                preg_match(model[QString().setNum(i)].toMap()["columns"].toMap()["price"].toString(), lineArr[arrIndex]).size() > 0 &&
                                !result[QString().setNum(i)].toMap()["columns"].toMap()["price"].toString().compare("")){
                            if(result["start_line"].toInt() != -1){
                                result["start_line"] = row + 1;
                            }
                            _tmp1 = result["columns"].toMap();
                            _tmp1["price"] = column;
                            result["columns"] = _tmp1;

                            _tmp1 = result["settings"].toMap();
                            _tmp1["price_zero"] = model[QString().setNum(i)].toMap()["columns"].toMap()["price_zero"];
                            result["settings"] = _tmp1;
                            col--;
                        }
                        if(model[QString().setNum(i)].toMap()["columns"].toMap()["rpp"] != QVariant::Invalid &&
                                preg_match(model[QString().setNum(i)].toMap()["columns"].toMap()["rpp"].toString(), lineArr[arrIndex]).size() > 0 &&
                                !result[QString().setNum(i)].toMap()["columns"].toMap()["rpp"].toString().compare("")){
                            if(result["start_line"].toInt() != -1){
                                result["start_line"] = row + 1;
                            }
                            _tmp1 = result["columns"].toMap();
                            _tmp1["rrp"] = column;
                            result["columns"] = _tmp1;

                            _tmp1 = result["settings"].toMap();
                            _tmp1["price_zero"] = model[QString().setNum(i)].toMap()["columns"].toMap()["price_zero"];
                            result["settings"] = _tmp1;
                            col--;
                        }
                        if(model[QString().setNum(i)].toMap()["columns"].toMap()["comment"] != QVariant::Invalid &&
                                preg_match(model[QString().setNum(i)].toMap()["columns"].toMap()["comment"].toString(), lineArr[arrIndex]).size() > 0 &&
                                !result[QString().setNum(i)].toMap()["columns"].toMap()["comment"].toString().compare("")){
                            if(result["start_line"].toInt() != -1){
                                result["start_line"] = row + 1;
                            }
                            _tmp1 = result["columns"].toMap();
                            _tmp1["comment"] = column;
                            result["columns"] = _tmp1;
                            col--;
                        }
                        if(model[QString().setNum(i)].toMap()["columns"].toMap()["quantity"] != QVariant::Invalid) {
                            for(int k = 0; k < model[QString().setNum(i)].toMap()["columns"].toMap()["quantity"].toStringList().size(); k++){
//                                qDebug() << "1  " << ((model[QString().setNum(i)].toMap()["columns"].toMap()["quantity"].toStringList()[k].length() > 0) ? "true" : "false") << "   " << model[QString().setNum(i)].toMap()["columns"].toMap()["quantity"].toStringList()[k];
//                                qDebug() << "2  " << ((preg_match(model[QString().setNum(i)].toMap()["columns"].toMap()["quantity"].toStringList()[k], lineArr[arrIndex]).size() > 0) ? "true" : "false") << "  " << lineArr[arrIndex];
//                                qDebug() << "3  " << (!result[QString().setNum(i)].toMap()["columns"].toMap()["quantity"].toStringList().size() >= k ? "true" : "false");
//                                qDebug() << "LINE: " << lineArr[arrIndex] << "  QUANTITY[K]: " << model[QString().setNum(i)].toMap()["columns"].toMap()["quantity"].toStringList()[k];
                                if(model[QString().setNum(i)].toMap()["columns"].toMap()["quantity"].toStringList()[k].length() > 0 &&
                                        preg_match(model[QString().setNum(i)].toMap()["columns"].toMap()["quantity"].toStringList()[k], lineArr[arrIndex]).size() > 0 &&
                                        !result[QString().setNum(i)].toMap()["columns"].toMap()["quantity"].toStringList().size() >= k) {
                                    qDebug() << "LINE: " << lineArr[arrIndex] << "  QUANTITY[K]: " << model[QString().setNum(i)].toMap()["columns"].toMap()["quantity"].toStringList()[k] << "   " << arrIndex << "   r: " << row;
                                    if(result["start_line"].toInt() != -1){
                                        result["start_line"] = row + 1;
                                    }
                                    _tmp1 = result["columns"].toMap();
                                    __tmp1 = _tmp1["quantity"].toStringList();
                                    __tmp1.push_back(QString().setNum(column));
                                    _tmp1["quantity"] = __tmp1;
                                    result["columns"] = _tmp1;

                                    _tmp1 = result["settings"].toMap();
                                    _tmp1["quantity_zero"] = model[QString().setNum(i)].toMap()["settings"].toMap()["quantity_zero"].toString();
                                    result["settings"] = _tmp1;
                                    col--;
                                }
                            }
                        }
                        if(model[QString().setNum(i)].toMap()["columns"].toMap()["more_comment"] != QVariant::Invalid){
                            for(int k = 0; k < model[QString().setNum(i)].toMap()["columns"].toMap()["more_comment"].toMap().size(); k++){
                                if(model[QString().setNum(i)].toMap()["columns"].toMap()["more_comment"].toMap()[QString().setNum(k)] != QVariant::Invalid &&
                                        preg_match(model[QString().setNum(i)].toMap()["columns"].toMap()["more_comment"].toMap()[QString().setNum(k)].toString(), lineArr[arrIndex]).size() > 0 &&
                                        !result[QString().setNum(i)].toMap()["columns"].toMap()["more_comment"].toMap()[QString().setNum(k)].toString().compare("")){
                                    if(result["start_line"].toInt() != -1){
                                        result["start_line"] = row + 1;
                                    }
                                    _tmp1 = result["columns"].toMap();
                                    _tmp2 = _tmp1["more_comment"].toMap();
                                    _tmp2[QString().setNum(k)] = column;
                                    _tmp1["more_comment"] = _tmp2;
                                    result["columns"] = _tmp1;
                                    col--;
                                }
                            }
                        }
                    }
//                    if(col==0)
//                        break;
                }
                if(model[QString().setNum(i)].toMap()["relative_columns"].toMap()["name"].toString().length() > 0) {
                    _tmp1 = result["columns"].toMap();

                    _tmp1["name"] = relative_str(result, result["columns"].toMap()["name"].toString(), result["relative_columns"].toMap()["name"].toString());
                    _tmp1["article"] = relative_str(result, result["columns"].toMap()["article"].toString(), result["relative_columns"].toMap()["article"].toString());
                    _tmp1["code"] = relative_str(result, result["columns"].toMap()["code"].toString(), result["relative_columns"].toMap()["code"].toString());
                    _tmp1["price"] = relative_str(result, result["columns"].toMap()["price"].toString(), result["relative_columns"].toMap()["price"].toString());
                    _tmp1["rrp"] = relative_str(result, result["columns"].toMap()["rrp"].toString(), result["relative_columns"].toMap()["rrp"].toString());

                    result["columns"] = _tmp1;

                    for(int z = 0;z < model[QString().setNum(i)].toMap()["relative_columns"].toMap()["quantity"].toStringList().size(); z++) {
                        _tmp1 = result["columns"].toMap();
                        __tmp1 = _tmp1["quantity"].toStringList();
                        __tmp1[z] = relative_str(result, result["columns"].toMap()["quantity"].toStringList()[z], result["relative_columns"].toMap()["quantity"].toStringList()[z]);
                        _tmp1["quantity"] = __tmp1;
                        result["columns"] = _tmp1;
                    }
                    for(int z = 0;z < model[QString().setNum(i)].toMap()["relative_columns"].toMap()["more_comment"].toMap().size(); z++) {
                        _tmp1 = result["columns"].toMap();
                        _tmp2 = _tmp1["more_comment"].toMap();
                        __tmp1[z] = relative_str(result, result["columns"].toMap()["more_comment"].toMap()[QString().setNum(z)].toString(), result["relative_columns"].toMap()["quantity"].toStringList()[z]);
                        _tmp1["more_comment"] = __tmp1;
                        result["columns"] = _tmp1;
                    }
                }

                if(!result["settings"].toMap()["merge_type"].toString().compare("name") ||
                        !result["settings"].toMap()["merge_type"].toString().compare("name_comment")){
                    if(result["columns"].toMap()["name"].toString().length() <= 0 ||
                            (result["columns"].toMap()["price"].toString().length() <= 0 && result["columns"].toMap()["rrp"].toString().length() <= 0 && result["columns"].toMap()["quantity"].toStringList().size() <= 0)) {
                        result["error"] = "101";
                    }
                }
                else if(!result["settings"].toMap()["merge_type"].toString().compare("name_article") ||
                        !result["settings"].toMap()["merge_type"].toString().compare("name_article_comment")) {
                    if(result["columns"].toMap()["name"].toString().length() <= 0 || result["columns"].toMap()["article"].toString().length() <= 0 ||
                            (result["columns"].toMap()["price"].toString().length() <= 0 && result["columns"].toMap()["rrp"].toString().length() <= 0 && result["columns"].toMap()["quantity"].toStringList().size() <= 0)) {
                        result["error"] = "102";
                    }
                }
                else if(!result["settings"].toMap()["merge_type"].toString().compare("name_code")) {
                    if(result["columns"].toMap()["name"].toString().length() <= 0 || result["columns"].toMap()["code"].toString().length()<=0 ||
                            (result["columns"].toMap()["price"].toString().length() <= 0 && result["columns"].toMap()["rrp"].toString().length() <= 0 && result["columns"].toMap()["quantity"].toStringList().size() <= 0)) {
                        result["error"] = "103";
                    }
                }
                else if(!result["settings"].toMap()["merge_type"].toString().compare("article") ||
                        !result["settings"].toMap()["merge_type"].toString().compare("article_comment")){
                    if(result["columns"].toMap()["article"].toString().length() <= 0 || result["columns"].toMap()["comment"].toString().length() <= 0 ||
                            (result["columns"].toMap()["price"].toString().length() <= 0 && result["columns"].toMap()["rrp"].toString().length() <= 0 && result["columns"].toMap()["quantity"].toStringList().size() <= 0)) {
                        result["error"] = "104";
                    }
                }
                else if(!result["settings"].toMap()["merge_type"].toString().compare("code")) {
                    if(result["columns"].toMap()["code"].toString().length() <= 0 ||
                            (result["columns"].toMap()["price"].toString().length() <= 0 && result["columns"].toMap()["rrp"].toString().length() <= 0 && result["columns"].toMap()["quantity"].toStringList().size() <= 0)){
                        result["error"] = "105";
                    }
                }
                else if(!result["settings"].toMap()["merge_type"].toString().compare("all")) {
                    if(result["columns"].toMap()["name"].toString().length() <= 0 || result["columns"].toMap()["article"].toString().length() <= 0 ||
                            result["columns"].toMap()["code"].toString().length() <= 0 || (result["columns"].toMap()["price"].toString().length() <= 0
                                                                                           && result["columns"].toMap()["rrp"].toString().length() <= 0 && result["columns"].toMap()["quantity"].toStringList().size() <= 0)) {
                        result["error"] = "106";
                    }
                }
                else if(!result["settings"].toMap()["merge_type"].toString().compare("article_code")){
                    if(result["columns"].toMap()["article"].toString().length() <= 0 || result["columns"].toMap()["code"].toString().length() <= 0 ||
                            (result["columns"].toMap()["price"].toString().length() <= 0 && result["columns"].toMap()["rrp"].toString().length() <= 0 && result["columns"].toMap()["quantity"].toStringList().size() <= 0)) {
                        result["error"] = "107";
                    }
                }
                else
                    result["error"] = "2";
                if(result["error"].toString().length() == 0)
                    break;
                else
                    result["error_rus"] = ErrorHandler::ERROR[result["error"].toString()];
            }
        }
    }
    return result;
}

void DataParser::get_new_csv(QString column, QString m_csv_name)
{
    QVector<QString> matches;
    QString column_name;
    column = column.trimmed();
    matches = relative_reg(".+", column);

    if(matches[0].length() > 0) {
        column_name = matches[0] + "_csv";
        if(!m_csv["column_name"].toString().length()) {
            m_csv["column_name"] = 0;
        }
        if(matches[1] == "+") {
            m_csv["csv_name"] = m_csv["column_name"].toInt() + matches[2].toInt();
        } else if(matches[1] == "-") {
            m_csv["csv_name"] = m_csv["column_name"].toInt() - matches[2].toInt();
        } else if(matches[1] == "*") {
            m_csv["csv_name"] = m_csv["column_name"].toInt() * matches[2].toInt();
        } else if(matches[1] == "/") {
            QString ss = QString::number((float)((float)m_csv["column_name"].toFloat() / (float)matches[2].toFloat()) , 'f');
            m_csv["csv_name"] = ss;
        } else if(matches[1] == ".") {
            m_csv["csv_name"] = m_csv["column_name"].toString() + "." + matches[2];
        }
    }
}

int DataParser::continuePricelistString()
{
    int continue_ = 0;
    int empty_data = 0;
    bool data_can_be_empty = true;

    int empty_data_total_conditions = 3;
    int empty_data_true_conditions = 0;

    QVariantMap tmp_csv = m_csv_data;

    QString merge_type = m_pricelist_config["settings"].toMap()["merge_type"].toString();

    QVector<QString> target_field_name;
    QVector<QString> target_field_csv;

    for(int k = 0; k < m_service_mode.size(); k++) {
        if(m_service_mode[k] == 4) {
            QString pricelist_settings_service_mark_target_value =
                    m_pricelist_config["service_marks"].toMap()[QString().setNum(k)].toMap()["main"].toMap()["target"].toString();
            if(pricelist_settings_service_mark_target_value.length() == 0) {
                pricelist_settings_service_mark_target_value =
                        m_pricelist_config["service_marks"].toMap()[QString().setNum(k)].toMap()["main"].toMap()["source"].toString();
                if(pricelist_settings_service_mark_target_value.length() == 0) {
                    pricelist_settings_service_mark_target_value = "name";
                }
            }
            target_field_name.push_back(pricelist_settings_service_mark_target_value + "_csv");
            target_field_name.push_back(m_prev_name[k] + " " + m_csv[target_field_name[k]].toString());
        }
    }

    if(m_pricelist_config["settings"].toMap()["price_zero"].toString().length() > 0) {
        empty_data_total_conditions--;
        if(m_pricelist_config["columns"].toMap()["price"].toString().length() > 0) {
            data_can_be_empty = false;
        }
    } else {
        if(1/* array search  != false*/) {
            int key = 0;// array search
            if(target_field_csv[key] == "") {
                empty_data_true_conditions++;
            }
        } else if(m_csv["price_csv"] == "") {
            empty_data_true_conditions++;
        }
    }

    if(m_pricelist_config["settings"].toMap()["quantity_zero"].toString().length() > 0) {
        empty_data_total_conditions--;
        if(m_pricelist_config["columns"].toMap()["quantity"].toStringList().size() > 0) {
            data_can_be_empty = false;
        }
    } else {
        if(1/* array search*/) {
            int key = 1;// array_search
            if(target_field_csv[key] == "") {
                empty_data_true_conditions++;
            }
        } else if(m_csv["quantity_csv"] == "") {
            empty_data_true_conditions++;
        }
    }

    if(m_pricelist_config["settings"].toMap()["rrp_zero"].toString().length() > 0) {
        empty_data_total_conditions--;
        if(m_pricelist_config["columns"].toMap()["rrp"].toString().length() > 0){
            data_can_be_empty = false;
        }
    } else {
        if(1/* array search != false*/) {
            int key = 0;// array seaech
            if(target_field_csv[key] == "") {
                empty_data_true_conditions++;
            }
        }
        else if(m_csv["rrp_csv"] == "") {
            empty_data_true_conditions++;
        }
    }

    if(empty_data_true_conditions == empty_data_total_conditions && data_can_be_empty) {
        empty_data = 1;
    }

    if(empty_data) {
        continue_ = 1;
    } else if(1 /* in array name name_comment*/) {
        if(1 /* as name_csv tfn*/) {
            int key = 0; // as
            if(target_field_csv[key] == "") {
                continue_ = 1;
            }
        }
    } else if(1 /*in array*/) {
        if(1 /*array search name_csv*/) {
            int key = 1;// array search
            if(target_field_csv[key] == "") {
                continue_ = 1;
            }
        } else if(1 /*as article_csv*/) {
            int key = 0;// as articls_csv
            if(target_field_csv[key] == "") {
                continue_ = 1;
            }
        } else {
            if(m_csv["name_csv"].toString() == "" && m_csv["article_csv"].toString() == "") {
                continue_ = 1;
            }
        }
    } else if(1 /*ia name_code*/) {
        if(1 /*as  name_csv*/) {
            int key = 1; //as nmae_csv
            if(target_field_csv[key] == "") {
                continue_ = 1;
            }
        } else if(1 /*as code_csv*/) {
            int key = 0; //as code
            if(target_field_csv[key] == "") {
                continue_ = 1;
            }
        } else {
            if(m_csv["name_csv"].toString() == "" && m_csv["code_csv"].toString() == "") {
                continue_ = 1;
            }
        }
    }
    else if(1 /*in array article_code*/) {
        if(1 /*as article_csv*/) {
            int key = 0; //as article
            if(target_field_csv[key] == "") {
                continue_ = 1;
            }
        } else if(1 /*as code*/) {
            int key = 0; //as code
            if(target_field_csv[key] == ""){
                continue_ = 1;
            }
        } else {
            if(m_csv["article_csv"].toString() == "" && m_csv["code_csv"].toString() == "") {
                continue_ = 1;
            }
        }
    }
    else if(1 /*ia article comment*/) {
        if(1 /*as article_csv*/) {
            int key = 0; //as article_csv
            if(target_field_csv[key] == ""){
                continue_ = 1;
            }
        }
    }
    else if(1 /*in array code*/) {
        if(1 /*as code_csv*/) {
            int key = 0; //as code_csv
            if(target_field_csv[key] == ""){
                continue_ = 1;
            }
        }
    } else if(1 /* ia all*/) {
        if(m_csv["name_csv"].toString() == "" && m_csv["article_csv"].toString().length() > 0 && m_csv["code_csv"].toString().length() > 0) {
            continue_ = 1;
        }
    }
    else {
        continue_ = 2;
    }
    return continue_;
}

void DataParser::serviceMarkSettingsProcessing(int i, bool from_five_to_four)
{
    QVariantMap _tmp1, _tmp2, _tmp3;
    for(int k = 0; k < m_service_mark_settings.size(); k++) {
        QString new_name;
        int p_tmp_first_index;
        int continue_ = 0;
        QVector<QVariantMap> m_pricelist_tmp_tmp;

        if(m_csv["service_mark_csv"] == "4" && m_service_mode[k] != 4) {
            m_pricelist_tmp[QString().setNum(k)] = QVariantMap();
            _tmp1 = m_pricelist_tmp[QString().setNum(k)].toMap();
            _tmp2 = _tmp1[QString().setNum(i)].toMap();
            _tmp2["first"] = "1";
            _tmp1[QString().setNum(i)] = _tmp2;
            m_pricelist_tmp[QString().setNum(k)] = _tmp1;

            m_service_mode[k] = 4;
            m_prev_name[k] = m_prev_field[k];
            m_prev_name_replaced[k] = m_prev_field_replaced[k];
            m_start_collecting_four[k] = true;
        }

        if(m_service_mode[k] == 4 && m_start_collecting_four[k]) {
            if(m_csv["service_mark_csv"].toString().length() > 0) {
                if(m_service_mark_settings[k]["four_type"].toString() == "2") {
                    new_name = "";
                    p_tmp_first_index = 0;
                    for(int p_tmp_index = 0; p_tmp_index < m_pricelist_tmp[QString().setNum(k)].toMap().size(); p_tmp_index++) {
                        if(!p_tmp_first_index) {
                            p_tmp_first_index = p_tmp_index;
                        }
                        new_name += " " + m_pricelist_tmp[QString().setNum(k)].toMap()[QString().setNum(p_tmp_index)].toMap()["name"].toString();
                    }
                    new_name = new_name.trimmed();
                    QVariantMap new_pricelist_tmp;
                    new_pricelist_tmp = m_pricelist_tmp[QString().setNum(k)].toMap()[QString().setNum(p_tmp_first_index)].toMap();
                    new_pricelist_tmp["name"] = new_name;
                    new_pricelist_tmp["first"] = "";
                    m_pricelist_tmp[QString().setNum(k)] = new_pricelist_tmp;

                    m_csv_data.empty();
                    m_csv_data["price_csv"]     = m_pricelist_tmp[QString().setNum(k)].toMap()[QString().setNum(p_tmp_first_index)].toMap()["price"].toString();
                    m_csv_data["quantity_csv"]  = m_pricelist_tmp[QString().setNum(k)].toMap()[QString().setNum(p_tmp_first_index)].toMap()["quantity"].toString();
                    m_csv_data["rrp_csv"]       = m_pricelist_tmp[QString().setNum(k)].toMap()[QString().setNum(p_tmp_first_index)].toMap()["rrp"].toString();
                    m_csv_data["name_csv"]      = m_pricelist_tmp[QString().setNum(k)].toMap()[QString().setNum(p_tmp_first_index)].toMap()["name"].toString();
                    m_csv_data["article_csv"]   = m_pricelist_tmp[QString().setNum(k)].toMap()[QString().setNum(p_tmp_first_index)].toMap()["article"].toString();
                    m_csv_data["code_csv"]      = m_pricelist_tmp[QString().setNum(k)].toMap()[QString().setNum(p_tmp_first_index)].toMap()["code"].toString();
                    m_csv_data["comment_csv"]   = m_pricelist_tmp[QString().setNum(k)].toMap()[QString().setNum(p_tmp_first_index)].toMap()["comment"].toString();

                    continue_ = continuePricelistString();

                    /*
                    for()
                    */
                    m_line_of_text.empty();
                    m_line_of_text.push_back(m_pricelist_config["columns"].toMap()["name"].toString());
                    m_line_of_text.push_back(m_pricelist_config["columns"].toMap()["article"].toString());
                    m_line_of_text.push_back(m_pricelist_config["columns"].toMap()["code"].toString());
                    m_line_of_text.push_back(m_pricelist_config["columns"].toMap()["price"].toString());
                    for(int j = 0; j < m_pricelist_config["columns"].toMap()["quantity"].toStringList().size(); j++) {
                        m_line_of_text.push_back(m_pricelist_config["columns"].toMap()["quantity"].toStringList()[j]);
                    }
                    m_line_of_text.push_back(m_pricelist_config["columns"].toMap()["rrp"].toString());
                    m_line_of_text.push_back(m_pricelist_config["columns"].toMap()["comment"].toString());
                    for(int j = 0; j < m_pricelist_config["columns"].toMap()["more_comment"].toMap().size(); j++) {
                        m_line_of_text.push_back(m_pricelist_config["columns"].toMap()["more_comment"].toMap()[QString().setNum(j)].toString());
                    }
                }
                else {
                    for(int p_tmp_index = 0; p_tmp_index < m_pricelist_tmp[QString().setNum(k)].toMap().size(); p_tmp_index++) {
                        if(m_service_mark_settings[k]["get_first"].toString().length() > 0) {
                            if(m_pricelist_tmp[QString().setNum(k)].toMap().size() > 1 &&
                                    m_pricelist_tmp[QString().setNum(k)].toMap()[QString().setNum(p_tmp_index)].toMap()["first"].toString().length() > 0) {
                                _tmp1 = m_pricelist_tmp[QString().setNum(k)].toMap();
                                _tmp1[QString().setNum(p_tmp_index)] = "";
                                m_pricelist_tmp[QString().setNum(k)] = _tmp1;
                            }
                        }
                        if((m_pricelist_tmp[QString().setNum(k)].toMap()[QString().setNum(p_tmp_index)].toMap()["first"].toString().length() > 0 &&
                                m_service_mark_settings[k]["get_first"].toString().length() > 0) ||
                                m_pricelist_tmp[QString().setNum(k)].toMap()[QString().setNum(p_tmp_index)].toMap()["first"].toString().length() == 0) {
                            if(m_service_mark_settings[k]["target"].toString() == "name") {
                                _tmp1 = m_pricelist_tmp[QString().setNum(k)].toMap();
                                _tmp2 = _tmp1[QString().setNum(p_tmp_index)].toMap();
                                _tmp2["name_original"] = m_pricelist_tmp[QString().setNum(k)].toMap()[QString().setNum(p_tmp_index)].toMap()["name"].toString();
                                _tmp1[QString().setNum(p_tmp_index)] = _tmp2;
                                m_pricelist_tmp[QString().setNum(k)] = _tmp1;
                                m_pricelist[p_tmp_index]["name_original"] = "";
                            }
                            QString target = m_service_mark_settings[k]["target"].toString();
                            QString target_replaced_field;

                            QVariantMap tmap;
                            m_pricelist_tmp_tmp.push_back(tmap);
                            if(target == "price" || target == "quantity" || target == "rrp") {
                                m_pricelist_tmp_tmp[p_tmp_index]["target"] = m_prev_name[k];
                            } else {
                                m_pricelist_tmp_tmp[p_tmp_index]["target"] = m_prev_name[k] + " " + m_pricelist_tmp[QString().setNum(k)].toMap()[QString().setNum(p_tmp_index)].toMap()["target"].toString();
                            }
                            if(target == "name" || target == "article" || target == "code") {
                                target_replaced_field = target + "_replaced";
                                m_pricelist_tmp_tmp[p_tmp_index][target_replaced_field] = "";//tolower() ???
                            }
                            //for(/*array keys*/) {};

                        }
                        if(m_pricelist_tmp[QString().setNum(k)].toMap()[QString().setNum(p_tmp_index)].toMap()["first"].toString().length() > 0) {
                            _tmp1 = m_pricelist_tmp[QString().setNum(k)].toMap();
                            _tmp2 = _tmp1[QString().setNum(p_tmp_index)].toMap();
                            _tmp2["first"] = "";
                            _tmp1[QString().setNum(p_tmp_index)] = _tmp2;
                            m_pricelist_tmp[QString().setNum(k)] = _tmp1;
                        }
                    }
                    if(m_pricelist_tmp[QString().setNum(k)].toString().size() > 0) {
                        m_pricelist_tmp[QString().setNum(k)] = "";
                    }
                }
                if(!continue_) {
                    if(m_pricelist_tmp_tmp.size() > 0) {
                        for(int ind = 0; ind < m_pricelist_tmp_tmp.size(); ind++) {
                            for(QVariantMap::const_iterator it = m_pricelist_tmp_tmp[ind].begin();
                                it != m_pricelist_tmp_tmp[ind].end(); it++) {
                                m_pricelist[ind][it.key()] = it.value();
                            }
                        }
                        m_pricelist_tmp_tmp.empty();
                    }
                    if(m_pricelist_tmp[QString().setNum(k)].toMap().size() > 0) {
                        for(int ind = 0; ind < m_pricelist_tmp[QString().setNum(ind)].toMap().size(); ind++) {
                            for(QVariantMap::const_iterator it = m_pricelist_tmp[QString().setNum(k)].toMap()[QString().setNum(ind)].toMap().begin();
                                    it != m_pricelist_tmp[QString().setNum(k)].toMap()[QString().setNum(ind)].toMap().end(); it++) {
                                m_pricelist[ind][it.key()] = it.value().toString();
                            }
                        }
                        m_pricelist_tmp[QString().setNum(k)] = "";
                    }
                }

                if(m_csv["service_mark_csv"].toString() != "5" || from_five_to_four) {
                    m_service_mode[k] = 0;
                    m_pricelist_tmp[QString().setNum(k)] = "";
                }
                if(m_csv["service_mark_csv"].toString() == "4") {
                    _tmp1 = m_pricelist_tmp[QString().setNum(k)].toMap();
                    _tmp2 = _tmp1[QString().setNum(i)].toMap();
                    _tmp2["first"] = "1";
                    _tmp1[QString().setNum(i)] = _tmp2;
                    m_pricelist_tmp[QString().setNum(k)] = _tmp1;
                    m_service_mode[k] = 4;
                    m_prev_name[k] == m_prev_field[k];
                    m_prev_name_replaced[k] = m_prev_field_replaced[k];
                    m_start_collecting_four[k] = true;
                }
            }
        }
    }
}

int DataParser::uploadPricelist(QString file, int id, QString filename, QVector<QString> user_config)
{
    QVariantMap _tmp1, _tmp2, _tmp3;
    QVector<QString> cmatch;
    cmatch = relative_reg("([a-z]+|[+-]|[0-9]+)","article -   5");
    //cout<<cmatch[0]<<endl<<cmatch[1]<<endl<<cmatch[2]<<endl;

    int i, total_strings = 0, row;
    //map<string, string> m_csv; global
    int quantity_csv = -1;
    QVector<QString> more_comment_csv1, matches;

    QVector<QString> line_csv;
    // Result m_pricelist_config // global
    m_pricelist_config = checkPricelistTemplate(filename);
    QVariantMap result;
    if(m_pricelist_config["error"].toString().length() == 0) {
        result["error"] = m_pricelist_config["error"];
        return 0;
    }
    if(m_pricelist_config["settings"].toMap()["merge_type"].toString().length() > 0 &&
            m_pricelist_config["settings"].toMap()["merge_type"].toString() != "none"){
        i = -1;
        QFile in(filename);
//        ifstream in(filename.c_str());
        QString line;
        row=0;
        QStringList line_arr;
        while(!in.atEnd()){
            line = in.readLine();
            row++;
            if(m_pricelist_config["start_line"].toInt() > 0 && row < m_pricelist_config["start_line"].toInt())
                continue;
            total_strings++;
            m_csv["name_csv"] = "";
            m_csv["name_csv_replaced"] = "";
            m_csv["article_csv"] = "";
            m_csv["article_csv_replaced"] = "";
            m_csv["code_csv"] = "";
            m_csv["code_csv_replaced"] = "";
            m_csv["price_csv"] = "";
            quantity_csv = -1;
            m_csv["rrp_csv"] = "";
            m_csv["comment_csv"] = "";
            m_csv["comment_csv_replaced"] = "";
            m_csv["service_mark_csv"] = "";

            line = line.replace("\"", "");
            line_arr = line.split(",");

            for(int i = 0; i < line_arr.size(); i++) {
                line_csv.push_back(line_arr[i]);
            }
            if(m_pricelist_config["columns"].toMap()["name"].toString().length() > 0 &&
                    line_csv[m_pricelist_config["columns"].toMap()["name"].toInt()] != "") {
                m_csv["name_csv"] = line_csv[m_pricelist_config["columns"].toMap()["name"].toInt()];
                m_csv["name_csv"] = m_csv["name_csv"].toString().trimmed();
                m_csv["name_csv"] = m_csv["name_csv"].toString().mid(0,500);
                m_csv["name_csv_replaced"] = m_csv["name_csv"].toString().toLower();
                if(m_pricelist_config["settings"].toMap()["name_out_of_stock"].toString().length() > 0) {
                    if(reg1(m_pricelist_config["settings"].toMap()["name_out_of_stock"].toString() ,line_csv[m_pricelist_config["columns"].toMap()["name"].toInt()])) {
                        if(quantity_csv == -1){
                            quantity_csv = 0;
                        }
                        m_csv["name_csv"] = m_pricelist_config["settings"].toMap()["name_out_of_stock"].toString().replace("", m_csv["name_csv"].toString());
                        m_csv["name_csv"] = m_csv["name_csv"].toString().mid(0,500);
                        m_csv["name_csv_replaced"] = m_csv["name_csv"].toString().toLower();
                    }
                    else
                        quantity_csv=1;
                }
            }
            if(m_pricelist_config["columns"].toMap()["article"].toString().length() > 0 && line_csv[m_pricelist_config["columns"].toMap()["article"].toInt()]!="") {
                m_csv["article_csv"] = line_csv[m_pricelist_config["columns"].toMap()["article"].toInt()];
                m_csv["article_csv"] = m_csv["article_csv"].toString().trimmed();
                m_csv["article_csv"] = m_csv["article_csv"].toString().mid(0,500);
                m_csv["article_csv_replaced"] = m_csv["article_csv"].toString().toLower();
            }
            if(m_pricelist_config["columns"].toMap()["code"].toString().length()>0 && line_csv[m_pricelist_config["columns"].toMap()["code"].toInt()]!="") {
                m_csv["code_csv"] = line_csv[m_pricelist_config["columns"].toMap()["code"].toInt()];
                m_csv["code_csv"] = m_csv["code_csv"].toString().trimmed();
                m_csv["code_csv"] = m_csv["code_csv"].toString().mid(0,500);
                m_csv["code_csv_replaced"] = m_csv["code_csv"].toString().toLower();
            }
            if(m_pricelist_config["columns"].toMap()["price"].toString().length() > 0 && line_csv[m_pricelist_config["columns"].toMap()["price"].toInt()]!=""){
                m_csv["price_csv"] = line_csv[m_pricelist_config["columns"].toMap()["price"].toInt()];
            }
            if(m_pricelist_config["columns"].toMap()["quantity"].toStringList().size()>0){
                for(int z = 0; z <= m_pricelist_config["columns"].toMap()["quantity"].toStringList().size(); z++){
                    if(m_pricelist_config["settings"].toMap()["quantity_in_stock"].toString().length()>0 &&
                    reg1(m_pricelist_config["settings"].toMap()["quantity_in_stock"].toString(), line_csv[m_pricelist_config["columns"].toMap()["quantity"].toStringList()[z].toInt()])) {
                        quantity_csv++;
                    }
                    else if(m_pricelist_config["settings"].toMap()["quantity_out_of_stock"].toString().length()>0 &&
                    reg1(m_pricelist_config["settings"].toMap()["quantity_out_of_stock"].toString(), line_csv[m_pricelist_config["columns"].toMap()["quantity"].toStringList()[z].toInt()])){
                        if(quantity_csv == -1)
                            quantity_csv = 0;
                    }
                    else{
                        matches = relative_reg("+",line_csv[m_pricelist_config["columns"].toMap()["quantity"].toStringList()[z].toInt()]);
                        if(matches.size()>0){
                            quantity_csv += matches[0].toInt();
                            if(matches[0].toInt() < 0 && quantity_csv == -1){
                                quantity_csv=0;
                            }
                        }
                    }
                }
            }
            if(m_pricelist_config["columns"].toMap()["rrp"].toString().length() > 0 && line_csv[m_pricelist_config["columns"].toMap()["rrp"].toInt()] != "") {
                m_csv["rrp_csv"] = line_csv[m_pricelist_config["columns"].toMap()["rrp"].toInt()];
            }
            if(m_pricelist_config["columns"].toMap()["comment"].toString().length() > 0 && line_csv[m_pricelist_config["columns"].toMap()["comment"].toInt()] != ""){
                m_csv["comment_csv"] = line_csv[m_pricelist_config["columns"].toMap()["comment"].toInt()];
                m_csv["comment_csv"] = m_csv["comment_csv"].toString().trimmed();
                m_csv["comment_csv"] = m_csv["comment_csv"].toString().mid(0, 500);
                m_csv["comment_csv_replaced"] = m_csv["comment_csv"].toString().toLower();
            }
            if(m_pricelist_config["columns"].toMap()["more_comment"].toMap().size() > 0){
                for(int z = 0; z <= m_pricelist_config["columns"].toMap()["more_comment"].toMap().size(); z++){
                    more_comment_csv1.push_back(line_csv[m_pricelist_config["columns"].toMap()["more_comment"].toMap()[QString().setNum(z)].toInt()]);
                    more_comment_csv1[z] = more_comment_csv1[z].trimmed();
                    more_comment_csv1.push_back(more_comment_csv1[z].mid(0,500));
                }
                m_csv["more_comment_csv"] = implode("_",more_comment_csv1);
                m_csv["comment_csv"] = m_csv["comment_csv"].toString() + "_" + m_csv["more_comment_csv"].toString();
                m_csv["comment_csv_replaced"] = m_csv["comment_csv"].toString().toLower();
            }


            //custom value process
            if(m_pricelist_config["custom_values"].toMap()["name"].toString().length() > 0) {
                get_new_csv(m_pricelist_config["custom_values"].toMap()["name, m_csv"].toString(), "name_csv");
            }
            if(m_pricelist_config["custom_values"].toMap()["article"].toString().length() > 0) {
                get_new_csv(m_pricelist_config["custom_values"].toMap()["article, m_csv"].toString(), "article_csv");
            }
            if(m_pricelist_config["custom_values"].toMap()["code"].toString().length() > 0) {
                get_new_csv(m_pricelist_config["custom_values"].toMap()["code, m_csv"].toString(), "code_csv");
            }
            if(m_pricelist_config["custom_values"].toMap()["price"].toString().length() > 0) {
                get_new_csv(m_pricelist_config["custom_values"].toMap()["price"].toString(), "price_csv");
            }
            if(m_pricelist_config["custom_values"].toMap()["quantity"].toStringList().size() > 0) {
                get_new_csv(m_pricelist_config["custom_values"].toMap()["quantity"].toString(), "quantity_csv");
            }
            if(m_pricelist_config["custom_values"].toMap()["rrp."].toString().length() > 0) {
                get_new_csv(m_pricelist_config["custom_values"].toMap()["rrp"].toString(), "rrp_csv");
            }
            if(m_pricelist_config["custom_values"].toMap()["comment"].toString().length() > 0) {
                get_new_csv(m_pricelist_config["custom_values"].toMap()["comment"].toString(), "comment_csv");
            }

            // service mark process
            QVariantMap service_mark_tmp;
            // vector<map<string, string>> service_mark_settings; global
            for(int index = 0; index < m_pricelist_config["service_marks"].toMap().size(); index++) {
                int true_conditions = 0;
                int total_conditions = 0;
                QString source = "";
                QString target = "";
                QString get_first = "";
                QString column_name;
                for(QVariantMap::const_iterator it = m_pricelist_config["service_marks"].toMap()[QString().setNum(index)].toMap()["main"].toMap().begin();
                        it != m_pricelist_config["service_marks"].toMap()[QString().setNum(index)].toMap()["main"].toMap().end(); ++it) {
                    if(it.key() == "type") {
                        service_mark_tmp["service_mark_four_type"] = it.value().toString();
                    } else if(it.key() == "source") {
                        service_mark_tmp["service_mark_source_tmp"] = it.value().toString();
                    } else if(it.key() == "target") {
                        service_mark_tmp["service_mark_target_tmp"] = it.value().toString();
                    } else if(it.key() == "get_first") {
                        get_first = it.value().toString();
                    } else {
                        total_conditions++;
                        column_name = it.key() + "_csv";
                        if(1/*preg_match("", "", m_csv[column_name])*/) {
                            true_conditions++;
                            }
                        }
                    }
                    if(true_conditions == total_conditions && total_conditions > 0) {
                        if(service_mark_tmp["service_mark_source_tmp"].toString().length() > 0) {
                            source = service_mark_tmp["service_mark_source_tmp"].toString();
                        } else {
                            source = "name";
                        }
                        if(service_mark_tmp["service_mark_target_tmp"].toString().length() > 0) {
                            target = service_mark_tmp["service_mark_target_tmp"].toString();
                        } else {
                            target = source;
                        }
                        m_csv["service_mark_csv"] = "4";
                        QVariantMap sms;
                        sms["four_type"] = service_mark_tmp["service_mark_four_type"].toString();
                        sms["source"] = source;
                        sms["target"] = target;
                        sms["get_first"] = get_first;
                        m_service_mark_settings.push_back(sms);
                        /// ????
                    } else if(/* ?? service mode ?? */ m_pricelist_config["service_marks"].toMap()[QString().setNum(index)].toMap()["extention"].toMap().size() > 0) {
                        true_conditions = 0;
                        total_conditions = 0;
                        for(QVariantMap::const_iterator it = m_pricelist_config["service_marks"].toMap()[QString().setNum(index)].toMap()["extention"].toMap().begin();
                            it != m_pricelist_config["service_marks"].toMap()[QString().setNum(index)].toMap()["extention"].toMap().end(); ++it) {
                                total_conditions++;
                            column_name = it.key() + "_csv";
                            if(1/*preg_match(), m_csv[column_name]*/) {
                                true_conditions++;
                            }
                        }
                        if(true_conditions == total_conditions && total_conditions > 0) {
                            if(service_mark_tmp["service_mark_source_tmp"].toString().length() > 0) {
                                source = service_mark_tmp["service_mark_source_tmp"].toString();
                            } else {
                                source = "name";
                            }
                            if(service_mark_tmp["service_mark_target_tmp"].toString().length()) {
                                target = service_mark_tmp["service_mark_target_tmp"].toString();
                            } else {
                                target = source;
                            }
                            m_csv["service_mark_csv"] = "5";
                            QVariantMap sms;
                            sms["four_type"] = service_mark_tmp["service_mark_four_type"].toString();
                            sms["source"] = source;
                            sms["target"] = target;
                            // sms["get_first"] = get_first;
                            m_service_mark_settings.push_back(sms);
                        }
                    }
                }
                // end service mark process
                // array decl
                // QVector<QString> m_prev_field;
                QVector<QString> m_prev_field_replaced;
                QVector<QString> service_mark_settings_four_type_array;
                for(int k = 0; k < m_service_mark_settings.size(); k++) {
                    QString prev_field_name = m_service_mark_settings[k]["source"].toString() + "_csv";
                    m_prev_field.push_back(m_csv[prev_field_name].toString());
                    service_mark_settings_four_type_array.push_back(m_service_mark_settings[k]["four_type"].toString());
                }
                // map<string, string> m_csv_data; // is global
                m_csv_data["price_csv"] = m_csv["price_csv"].toString();
                m_csv_data["quantity_csv"] = QString().setNum(quantity_csv);
                m_csv_data["rrp_csv"] = m_csv["rrp_csv"].toString();
                m_csv_data["name_csv"] = m_csv["name_csv"].toString();
                m_csv_data["code_csv"] = m_csv["code_csv"].toString();
                m_csv_data["comment_csv"] = m_csv["comment_csv"].toString();
                int continue_ = 0;

                if(m_csv["service_mark_csv"].toString() == "0") {
                    continue;
                }
                // m_start_collecting_four array?

                if(m_service_mark_settings.size() > 0) {
                    // serviceMarkSettingsProcessing(i)
                }

                if(1 /*in array*/) {
                    continue_ = 1/*continuePricelistString()*/;
                }

                if(!continue_ /* what ??*/) {
                    result["error"] = continue_;
                    // return;??
                }
                if(continue_) {
                    continue;
                }

                bool sm_true = true;
                if(m_csv["name_csv"].toString().compare("")){
                    sm_true = false;
                    for(int z = 0; z < m_service_mode.size(); z++){
                        if(m_service_mode[z] > 0){
                            sm_true=true;
                            _tmp1 = m_pricelist_tmp[QString().setNum(z)].toMap();
                            _tmp2 = _tmp1[QString().setNum(i)].toMap();
                            _tmp2["name"] = m_csv["name_csv"].toString();
                            _tmp2["name_replaced"] = m_csv["name_csv_replaced"].toString();
                            _tmp1[QString().setNum(i)] =_tmp2;
                            m_pricelist_tmp[QString().setNum(z)] = _tmp1;
                        }
                    }
                    if(sm_true == false){
                        m_pricelist[i]["name"] = m_csv["name_csv"].toString();
                        m_pricelist[i]["name_replaced"] = m_csv["name_csv_replaced"].toString();
                    }
                }
                if(m_csv["article_csv"].toString().compare("")){
                    sm_true = false;
                    for(int z = 0;z < m_service_mode.size(); z++){
                        if(m_service_mode[z] > 0){
                            sm_true = true;
                            _tmp2 = _tmp1[QString().setNum(i)].toMap();
                            _tmp1 = m_pricelist_tmp[QString().setNum(z)].toMap();
                            _tmp2["article"] = m_csv["article_csv"].toString();
                            _tmp2["article_replaced"] = m_csv["article_csv_replaced"].toString();
                            _tmp1[QString().setNum(i)] =_tmp2;
                            m_pricelist_tmp[QString().setNum(z)] = _tmp1;
                        }
                    }
                    if(sm_true == false){
                        m_pricelist[i]["article"] = m_csv["article_csv"].toString();
                        m_pricelist[i]["article_replaced"] = m_csv["article_csv_replaced"].toString();
                    }
                }

                if(m_csv["code_csv"].toString().compare("")){
                    sm_true = false;
                    for(int z = 0; z < m_service_mode.size(); z++){
                        if(m_service_mode[z] > 0){
                            sm_true=true;
                            _tmp2 = _tmp1[QString().setNum(i)].toMap();
                            _tmp1 = m_pricelist_tmp[QString().setNum(z)].toMap();
                            _tmp2["code"] = m_csv["code_csv"].toString();
                            _tmp2["code_replaced"] = m_csv["code_csv_replaced"].toString();
                            _tmp1[QString().setNum(i)] =_tmp2;
                            m_pricelist_tmp[QString().setNum(z)] = _tmp1;
                        }
                    }
                    if(sm_true == false){
                        m_pricelist[i]["code"] = m_csv["code_csv"].toString();
                        m_pricelist[i]["code_replaced"] = m_csv["code_csv_replaced"].toString();
                    }
                }
                if(m_csv["price_csv"].toString().compare("")){
                    sm_true = false;
                    for(int z = 0; z < m_service_mode.size() ; z++){
                        if(m_service_mode[z] > 0){
                            sm_true=true;
                            _tmp2 = _tmp1[QString().setNum(i)].toMap();
                            _tmp1 = m_pricelist_tmp[QString().setNum(z)].toMap();
                            _tmp2["price"] = m_csv["price_csv"].toString();
                            _tmp1[QString().setNum(i)] =_tmp2;
                            m_pricelist_tmp[QString().setNum(z)] = _tmp1;
                        }
                    }
                    if(sm_true == false){
                        m_pricelist[i]["price"] = m_csv["price_csv"].toString();
                    }
                }

                if(m_csv["quantity_csv"].toString().compare("")){
                    sm_true = false;
                    for(int z = 0; z < m_service_mode.size() ; z++){
                        if(m_service_mode[z] > 0){
                            sm_true=true;
                            _tmp2 = _tmp1[QString().setNum(i)].toMap();
                            _tmp1 = m_pricelist_tmp[QString().setNum(z)].toMap();
                            _tmp2["quantity"] = m_csv["quantity_csv"].toString();
                            _tmp1[QString().setNum(i)] =_tmp2;
                            m_pricelist_tmp[QString().setNum(z)] = _tmp1;
                        }
                    }
                    if(sm_true == false){
                        m_pricelist[i]["quantity"] = m_csv["quantity_csv"].toString();
                    }
                }

                if(m_csv["rrp_csv"].toString().compare("")){
                    sm_true = false;
                    for(int z = 0; z < m_service_mode.size() ; z++){
                        if(m_service_mode[z] > 0){
                            sm_true=true;
                            _tmp2 = _tmp1[QString().setNum(i)].toMap();
                            _tmp1 = m_pricelist_tmp[QString().setNum(z)].toMap();
                            _tmp2["rrp"] = m_csv["rrp_csv"].toString();
                            _tmp1[QString().setNum(i)] =_tmp2;
                            m_pricelist_tmp[QString().setNum(z)] = _tmp1;
                        }
                    }
                    if(sm_true == false){
                        m_pricelist[i]["rrp"] = m_csv["rrp_csv"].toString();
                    }
                }

                if(m_csv["comment_csv"].toString().compare("")){
                    sm_true = false;
                    for(int z = 0; z <m_service_mode.size(); z++){
                        if(m_service_mode[z]>0){
                            sm_true=true;
                            _tmp2 = _tmp1[QString().setNum(i)].toMap();
                            _tmp1 = m_pricelist_tmp[QString().setNum(z)].toMap();
                            _tmp2["comment"] = m_csv["comment_csv"].toString();
                            _tmp2["comment_replaced"] = m_csv["comment_csv_replaced"].toString();
                            _tmp1[QString().setNum(i)] =_tmp2;
                            m_pricelist_tmp[QString().setNum(z)] = _tmp1;
                        }
                    }
                    if(sm_true == false){
                        m_pricelist[i]["comment"] = m_csv["comment_csv"].toString();
                        m_pricelist[i]["comment_replaced"] = m_csv["comment_csv_replaced"].toString();
                    }
                }

                if(m_csv["service_mark_csv"].toString() == "5") {
                    serviceMarkSettingsProcessing(i, true);
                }

//				if(1 /*service_mode != "4" it is an array !!! */) {
//					if(m_csv["service_mark_csv"] == "1") {
//						pricelist_tmp.empty();
//						//service_mode = 1; array !!!
//						prev_name = m_prev_field;
//						prev_name_replaced = m_prev_field_replaced;
//					}
//					else if(m_csv["service_mark_csv"] == "2" /* &&m_service_mode  array !!!*/) {
//						for(int j = 0; j < m_pricelist_tmp[i].size(); j++) {
//							pricelist_tmp[i][j]["comment"] = m_prev_name;
//							pricelist_tmp[i][j]["comment_replaced"] = m_prev_name_replaced;
//						}
//						// pricelist = array_merge ??
//						//m_service_mode = 1 ??? ARRAY !!!
//						pricelist_tmp[i].empty();
//					}
//					else if(m_csv["service_mark_csv"] == "3" /* &&m_service_mode array !!! */) {
//						for(int j = 0; j < m_pricelist_tmp[i].size(); j++) {
//							pricelist_tmp[i][j]["name_original"] = m_pricelist_tmp[i][j]["name"];
//							pricelist_tmp[i][j]["name"] = m_prev_name + " " + m_pricelist_tmp[i][j]["name"];
//						}
//						// pricelist = array_merge ??
//						//m_service_mode = 0 ??? ARRAY !!!
//						pricelist_tmp[i].empty();
//					}
//					else {
//						sm_true = false;
//						for(int j = 0; j <m_service_mode.size(); j++) {
//							if(m_service_mode[j]) {
//								prev_name[j] = m_prev_field[j];
//								prev_name_replaced[j] = m_prev_field_replaced[j];
//							}
//						}
//					}
//				}

                QVector<QVariantMap> service_mark_settings_l;
                for(int sm_index = 0; sm_index < m_pricelist_config["service_marks"].toMap().size(); sm_index++) {
                    QString source = "";
                    QString target = "";
                    QString get_first = "";
                    if(m_pricelist_config["service_marks"].toMap()[QString().setNum(sm_index)].toMap()["main"].toMap().size() > 0) {
                        for(QVariantMap::const_iterator it = m_pricelist_config["service_marks"].toMap()[QString().setNum(sm_index)].toMap()["main"].toMap().begin();
                                it != m_pricelist_config["service_marks"].toMap()[QString().setNum(sm_index)].toMap()["main"].toMap().end(); it++) {
                            if(it.key() == "type") {
                                service_mark_tmp["service_mark_four_type"] = it.value().toString();
                            }
                            else if(it.key() == "source") {
                                source = it.value().toString();
                            }
                            else if(it.key() == "target") {
                                target = it.value().toString();
                            }
                            else if(it.key() == "get_first") {
                                get_first = it.value().toString();
                            }
                        }

                        if(source.length() == 0) {
                            source = "name";
                        }
                        if(target.length() == 0) {
                            target = source;
                        }

                        m_csv["service_mark_csv"] = "4";
                        m_service_mark_settings[sm_index]["four_type"] = service_mark_tmp["service_mark_four_type_tmp"].toString();
                        m_service_mark_settings[sm_index]["source"] = source;
                        m_service_mark_settings[sm_index]["target"] = target;
                        m_service_mark_settings[sm_index]["get_first"] = get_first;
                    }
                }
            }
            serviceMarkSettingsProcessing(i);

            if(m_pricelist_config["columns"].toMap()["quantity"].toList().size() > 0 && m_pricelist_config["settings"].toMap()["set_quantity_to_zero"].toBool()) {
                m_dManager.setQuery("SELECT pricelist_set_tech_zero_date FROM user WHERE id = '$id_supplier'");
                QString pud_res;
                if(m_dManager.next()) {
                    pud_res = m_dManager.getNext();
                }
                QString today = QDate::currentDate().toString();
                QString pricelist_set_tech_zero_date; // ??????
                if(today != pricelist_set_tech_zero_date) {
                    QVariantMap upd;
                    upd["quantity"] = 0;
                    upd["quantity_tech_zero"] = 1;
                    upd["quantity_date"] = QDate::currentDate().toString() + " " + QTime::currentTime().toString();
                    upd["quantity_user"] = current_user_id; // ???
                    m_dManager.prepareQuery("UPDATE pricelist " + upd + " WHERE id_supplier = " + id_supplier);
                    m_dManager.execQuery();
                    upd.empty();
                    upd["pricelist_set_tech_zero_date"] = QDate::currentDate().toString() + " " + QTime::currentTime().toString();
                    m_dManager.prepareQuery("UPDATE user " + upd + " WHERE id = " + id_supplier);
                    m_dManager.execQuery();

                    //addAllSuppliersPp(id_supplier); ???
                }
            }
            QVector<int> currency_values;
            currency_values.push_back(1);
            currency_values.push_back(getCurrencyValue(2));
            currency_values.push_back(getCurrencyValue(3));

            for(int pl = 0; pl < m_pricelist.size(); pl++) {
                QString name_replaced_sql = m_pricelist[pl]["name_replaced"].toString();
                QString article_replaced_sql = m_pricelist[pl]["article_replaced"].toString();
                QString code_replaced_sql = m_pricelist[pl]["code_replaced"].toString();
                QString comment_replaced_sql = m_pricelist[pl]["comment_replaced"].toString();

                QString info_fields = "id, status, price_date, quantity_date, currency, rrp_currency, discount_amount, id_product, id_pp";

                if(merge_type != -1) {
                    QString where = "";
                    for(int mp = 0; mp < merge_type_fields[merge_type].size(); mp++) {
                        replaced_field_sql_name = merge_type_fields[merge_type][mp] + "_replaced_sql";
                        where += " AND {" + merge_type_fields[merge_type][mp] + "}_replaced = '{" + replaced_field_sql_name + "}'"; // ???????
                    }
                    if(where.length() > 0) {
                        sql = "SELECT " + info_fields + "FROM pricelist WHERE 1 = 1 " + where + " AND id_supplier = " + id_supplier;
                    }
                } else {
                    if(m_pricelist_config["settings"].toMap()["merge_type"].toString() == "name_code") {
                        sql = "SELECT " + info_fields + " FROM pricelist WHERE name_replaced = '" + name_replaced_sql + "' AND id_supplier = " + id_supplier;
                    } else if(m_pricelist_config["settings"].toMap()["merge_type"].toString() == "name_article") {
                        sql = "SELECT " + info_fields + " FROM pricelist WHERE name_replaced = '" + name_replaced_sql + "' AND article_replaced = '"
                                + article_replaced_sql + "' AND id_supplier = " + id_supplier;
                    } else if(m_pricelist_config["settings"].toMap()["merge_type"].toString() == "code") {
                        sql = "SELECT " + info_fields + " FROM pricelist WHERE name_replaced = '" + name_replaced_sql + "' AND code_replaced = '"
                                + code_replaced_sql + "' AND id_supplier = " + id_supplier;
                    } else if(m_pricelist_config["settings"].toMap()["merge_type"].toString() == "article") {
                        sql = "SELECT " + info_fields + " FROM pricelist WHERE article_replaced = '" + article_replaced_sql + "' AND id_supplier = " + id_supplier;
                    } else if(m_pricelist_config["settings"].toMap()["merge_type"].toString() == "code") {
                        sql = "SELECT " + info_fields + " FROM pricelist WHERE code_replaced = '" + code_replaced_sql + "' AND id_supplier = " + id_supplier;
                    } else if(m_pricelist_config["settings"].toMap()["merge_type"].toString() == "article_code") {
                        sql = "SELECT " + info_fields + " FROM pricelist WHERE code_replaced = '" + code_replaced_sql + "' AND article_replaced = '" +
                                article_replaced_sql + "' AND id_supplier = " + id_supplier;
                    } else if(m_pricelist_config["settings"].toMap()["merge_type"].toString() == "all") {
                        sql = "SELECT " + info_fields +
                                " FROM pricelist WHERE name_replaced = '" + name_replaced_sql +
                                "' AND article_replaced = '" + article_replaced_sql +
                                "' AND code_replaced = '" + code_replaced_sql + "' AND id_supplier = " + id_supplier;
                    } else if(m_pricelist_config["settings"].toMap()["merge_type"].toString() == "name_comment") {
                        sql = sql = "SELECT " + info_fields + " FROM pricelist WHERE name_replaced = '" + name_replaced_sql + "' AND comment_replaced = '"
                                + comment_replaced_sql + "' AND id_supplier = " + id_supplier;
                    } else if(m_pricelist_config["settings"].toMap()["merge_type"].toString() == "article_comment") {
                        sql = sql = "SELECT " + info_fields + " FROM pricelist WHERE article_replaced = '" + article_replaced_sql + "' AND comment_replaced = '"
                                + comment_replaced_sql + "' AND id_supplier = " + id_supplier;
                    } else if(m_pricelist_config["settings"].toMap()["merge_type"].toString() == "name_article_comment") {
                        sql = sql = "SELECT " + info_fields + " FROM pricelist WHERE article_replaced = '"
                                + article_replaced_sql + "' AND comment_replaced = '"
                                + comment_replaced_sql + "' AND name_replaced = '"
                                + name_replaced_sql + "' AND id_supplier = " + id_supplier;
                    }

                    QString conn_slave; // = DBwork::$Slave; ???
                    // $res = $conn_slave->Query($sql); ????

                    QVector<QVariantMap> res; // ^^
                    QString id = res[0]["id"].toString();
                    QString price_date = res[0]["price_date"];
                    QString quantity_date = res[0]["quantity_date"];
                    QString status = res[0]["status"];
                    QString currency = res[0]["currency"];
                    QString rrp_currency = res[0]["rrp_currency"];
                    QString discount_amount = res[0]["discount_amount"];
                    QString id_product = res[0]["id_product"];

                    QVariantMap upd;
                    //upd = ???
                    if(upd["price"] != QVariant::Invalid) {
                        upd["price_original"] = upd["price"];
                        upd["price_date"] = QDate::currentDate().toString() + " " + QTime::currentTime().toString();
                        upd["price_user"] = current_user_id;
                    }
                    if(upd["quantity"] != QVariant::Invalid) {
                        upd["quantity_tech_zero"] = 0;
                        upd["quantity_date"] = QDate::currentDate().toString() + " " + QTime::currentTime().toString();
                        upd["quantity_user"] = current_user_id;
                    }
                    if(upd["rrp"] != QVariant::Invalid) {
                        upd["rrp_original"] = upd["rrp"];
                        upd["rrp_date"] = QDate::currentDate().toString() + " " + QTime::currentTime().toString();
                        upd["rrp_user"] = current_user_id;
                    }
                    upd["id_supplier"] = id_supplier;
                    upd["id_file"] = id_file; // ?????

                    if(res[0]["id_pp"])
                        id_pp_refresh[]=res[0]["id_pp"]; //??????

                    // res = false //??????

                    if(id) {
                        if(upd["price"] != QVariant::Invalid) {
                            upd["price"] = currency_values[currency] * upd["price_original"].toInt();
                        }
                        if(upd["rrp"] != QVariant::Invalid) {
                            upd["rrp"] = currency_values[currency] * upd["rrp_original"].toInt();
                        }
                        upd["id"] = id;
                        m_dManager.setQuery("UPDATE pricelist " + upd + " WHERE id = " + id);
                        if(m_dManager.next()) {
                            res = m_dManager.getNext();
                        }
                        if(id_product) {
                            // Product::setFixedPrice($id_product, $id); ?????
                        }
                    } else {
                        upd["date"] = Date::currentDate().toString() + " " + QTime::currentTime().toString();
                        upd["user"] = current_user_id;
                        upd["status_reason"] = "new";
                        if(m_pricelist_config["settings"].toMap()["price_discount"] != QVariant::Invalid) {
                            upd["discount_amount"] = m_pricelist_config["settings"].toMap()["price_discount"];
                            upd["discount_amount_user"] = current_user_id;
                            upd["discount_amount_date"] = upd["date"];
                            upd["discount_type"] = 10;
                            discount_amount = upd["discount_amount"];
                        }
                    }
                    // $conn->debug = 1; ???????? conn ??????
                    bool r;
                    m_dManager.prepareQuery("INSERT pricelist " + upd);
                    r = m_dManager.execQuery();
                    id = r;
                    if(r) {
                        QVariantMap ins_history;
                        ins_history["name"] = upd["name"];
                        ins_history["article"] = upd["article"];
                        ins_history["comment"] = upd["comment"];
                        ins_history["code"] = upd["code"];
                        ins_history["price"] = upd["price"];
                        ins_history["currency"] = currency;
                        ins_history["price_original"] = upd["price_original"];
                        ins_history["discount_amount"] = discount_amount;
                        ins_history["rrp"] = upd["rrp"];
                        ins_history["rrp_currency"] = rrp_currency;
                        ins_history["rrp_original"] = upd["rrp_original"];
                        ins_history["quantity"] = upd["quantity"];
                        ins_history["id_pricelist"] = id;
                        ins_history["date"] = QDate::currentDate().toString() + " " + QTime::currentTime().toString();
                        ins_history["user"] = current_user_id;
                        m_dManager.prepareQuery("INSERT 'pricelist_upload_history' " + ins_history);
                    }
                }
                // self::addPPToRefresh($id_pp_refresh); ????????
                if(semidoneExists){
                    result["semidone"] = 1;
                } else {
                    result["done"] = 1;
                }
                return result;
            } else {
                result["error"] = 2;
                return result;
            }
    } else {
        result["error"] = 5;
        return result;
    }
    return 0; // default
}
